﻿// dlg_main.cpp: 实现文件
//

#include "pch.h"
#include "CamCtrl.h"
#include "afxdialogex.h"
#include "dlg_main.h"
#include <spk_def.h>

// dlg_main 对话框

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")


IMPLEMENT_DYNAMIC(dlg_main, CDialogEx)

dlg_main::dlg_main(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MAIN_DLG, pParent)
{
	notify_wnd = nullptr;
}

dlg_main::~dlg_main()
{
}

void dlg_main::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TAB1, m_tab_ctrl);
}

BEGIN_MESSAGE_MAP(dlg_main, CDialogEx)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, &dlg_main::OnTcnSelchangeTab1)
	ON_MESSAGE(MSG_DEVICE_CTRL, &dlg_main::OnDeviceCtrlAck)
	ON_BN_CLICKED(IDC_BTN_CONFIRM, &dlg_main::OnBnClickedBtnConfirm)
	ON_BN_CLICKED(IDC_BTN_CANCEL, &dlg_main::OnBnClickedBtnCancel)
	ON_BN_CLICKED(IDC_BTN_APPLIY, &dlg_main::OnBnClickedBtnAppliy)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BTN_REFRESH, &dlg_main::OnBnClickedBtnRefresh)
	ON_BN_CLICKED(IDC_BTN_REBOOT, &dlg_main::OnBnClickedBtnReboot)
END_MESSAGE_MAP()

/*设备控制回应*/
LRESULT dlg_main::OnDeviceCtrlAck(WPARAM wParam, LPARAM lParam)
{
	struct device_info *info = (struct device_info*)wParam;
	
	if (info != nullptr)
	{
		set(info);
	}
	return 0;
}

void dlg_main::MoveToCenter(void)
{
	int iScreenW = GetSystemMetrics(SM_CXSCREEN);
	int iScreenH = GetSystemMetrics(SM_CYSCREEN);

	/* 获取窗口大小 */
	RECT rcWindowRect;
	GetWindowRect(&rcWindowRect);

	int iWindowW = rcWindowRect.right - rcWindowRect.left;
	int iWindowH = rcWindowRect.bottom - rcWindowRect.top;

	/* 获取任务栏高度 */
	RECT rect;
	HWND hwndTaskbar;
	int iTaskbarH;
	hwndTaskbar = ::FindWindow(_T("Shell_TrayWnd"), 0);
	::GetWindowRect(hwndTaskbar, &rect);
	iTaskbarH = rect.bottom - rect.top;

	int iLeft = (iScreenW - iWindowW) / 2;
	int iTop = (iScreenH - iWindowH - iTaskbarH) / 2;

	MoveWindow(iLeft, iTop, iWindowW, iWindowH, FALSE);
}


BOOL dlg_main::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	m_tab_ctrl.InsertItem(0, _T("网络配置"));
	m_tab_ctrl.InsertItem(1, _T("SIP配置"));
	m_tab_ctrl.InsertItem(2, _T("图像配置"));
	m_tab_ctrl.InsertItem(3, _T("抓拍配置"));
	m_tab_ctrl.InsertItem(4, _T("录像配置"));

	net_ctrl.Create(IDD_CAMCTRL_DIALOG, &m_tab_ctrl);
	sip_ctrl.Create(IDD_DLG_SIP_CTRL, &m_tab_ctrl);
	img_ctrl.Create(IDD_IMG_CTRL, &m_tab_ctrl);
	capture_ctrl.Create(IDD_DLG_CAPTURE_CTRL, &m_tab_ctrl);
	record_ctrl.Create(IDD_DLG_RECORD_CTRL, &m_tab_ctrl);

	CRect rc;
	CRect rcTabItem;

	m_tab_ctrl.GetClientRect(&rc);
	m_tab_ctrl.GetItemRect(0, rcTabItem);

	rc.top    += rcTabItem.Height() + 1; /*39+1*/
	rc.left   += 1;
	rc.bottom -= 1;
	rc.right  -= 1;

	net_ctrl.MoveWindow(&rc);
	sip_ctrl.MoveWindow(&rc);
	img_ctrl.MoveWindow(&rc);
	capture_ctrl.MoveWindow(&rc);
	record_ctrl.MoveWindow(&rc);

	m_tab_ctrl.SetCurFocus(0);

	net_ctrl.ShowWindow(SW_SHOW);
	net_ctrl.SetFocus();

	sip_ctrl.ShowWindow(SW_HIDE);
	img_ctrl.ShowWindow(SW_HIDE);
	capture_ctrl.ShowWindow(SW_HIDE);
	record_ctrl.ShowWindow(SW_HIDE);

	ShowWindow(SW_SHOWNORMAL);	
	resize.add(this);
	MoveToCenter();
	return TRUE;  
}

int dlg_main::set(struct device_info* info)
{
	net_ctrl.set(info);
	sip_ctrl.set(info);
	img_ctrl.set(info);
	capture_ctrl.set(info);
	record_ctrl.set(info);
	return 0;
}

int dlg_main::get(struct device_info* info)
{
	memset(info, 0, sizeof(struct device_info));
	net_ctrl.get(info);
	sip_ctrl.get(info);
	img_ctrl.get(info);
	capture_ctrl.get(info);
	record_ctrl.get(info);
	return 0;
}

BOOL dlg_main::valid(void)
{
	BOOL st = FALSE;
	st = net_ctrl.valid();
	if(st == FALSE) return st;
	st = sip_ctrl.valid();
	if (st == FALSE) return st;
	st = img_ctrl.valid();
	if (st == FALSE) return st;
	st = capture_ctrl.valid();
	if (st == FALSE) return st;
	st = record_ctrl.valid();
	return st;
}


void dlg_main::OnTcnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult)
{
	switch (m_tab_ctrl.GetCurSel())
	{
	case 0:
		sip_ctrl.ShowWindow(SW_HIDE);
		net_ctrl.ShowWindow(SW_SHOW);
		img_ctrl.ShowWindow(SW_HIDE);
		capture_ctrl.ShowWindow(SW_HIDE);
		record_ctrl.ShowWindow(SW_HIDE);
		net_ctrl.SetFocus();
	break;
	case 1:
		sip_ctrl.ShowWindow(SW_SHOW);
		net_ctrl.ShowWindow(SW_HIDE);
		img_ctrl.ShowWindow(SW_HIDE);
		capture_ctrl.ShowWindow(SW_HIDE);
		record_ctrl.ShowWindow(SW_HIDE);
		sip_ctrl.SetFocus();
	break;

	case 2:
		sip_ctrl.ShowWindow(SW_HIDE);
		net_ctrl.ShowWindow(SW_HIDE);
		img_ctrl.ShowWindow(SW_SHOW);
		capture_ctrl.ShowWindow(SW_HIDE);
		record_ctrl.ShowWindow(SW_HIDE);
		img_ctrl.SetFocus();
	break;

	case 3:
		sip_ctrl.ShowWindow(SW_HIDE);
		net_ctrl.ShowWindow(SW_HIDE);
		img_ctrl.ShowWindow(SW_HIDE);
		capture_ctrl.ShowWindow(SW_SHOW);
		record_ctrl.ShowWindow(SW_HIDE);
		capture_ctrl.SetFocus();
		break;

	case 4:
		sip_ctrl.ShowWindow(SW_HIDE);
		net_ctrl.ShowWindow(SW_HIDE);
		img_ctrl.ShowWindow(SW_HIDE);
		capture_ctrl.ShowWindow(SW_HIDE);
		record_ctrl.ShowWindow(SW_SHOW);
		record_ctrl.SetFocus();
		break;
	default:
		break;
	}
	*pResult = 0;
}

/*确认修改*/
void dlg_main::OnBnClickedBtnConfirm()
{
	if (!valid())
	{
		return;
	}

	int cmd = CMD_DEVICE_CTRL;
	struct device_info info;
	/*从本对话框获取待配置的信息*/
	get(&info);
	/*发送消息到设备管理对话框进行设备配置*/
	::SendMessage(notify_wnd,MSG_DEVICE_CTRL,(WPARAM)&info,(LPARAM)&cmd);
	/*本对话框退出*/
	::SendMessage(m_hWnd, WM_CLOSE, NULL, NULL);
	/*设备管理对话框退出*/
	//::SendMessage(notify_wnd, WM_CLOSE, NULL, NULL);
	cmd = CMD_DEVICE_CLOSE;
	::SendMessage(notify_wnd, MSG_DEVICE_CTRL, (WPARAM)NULL, (LPARAM)&cmd);
}

/*取消并关闭设备*/
void dlg_main::OnBnClickedBtnCancel()
{
	/*本对话框退出*/
	::SendMessage(m_hWnd, WM_CLOSE, NULL, NULL);
	/*设备管理对话框退出*/
	//::SendMessage(notify_wnd, WM_CLOSE, NULL, NULL);
	int cmd = CMD_DEVICE_CLOSE;
	::SendMessage(notify_wnd, MSG_DEVICE_CTRL, (WPARAM)NULL, (LPARAM)&cmd);
}

/*应用已修改的内容*/
void dlg_main::OnBnClickedBtnAppliy()
{
	int cmd = CMD_DEVICE_CTRL;
	struct device_info info;
	get(&info);
	::SendMessage(notify_wnd, MSG_DEVICE_CTRL, (WPARAM)&info, (LPARAM)&cmd);
	
	img_ctrl.Invalidate();//使整个窗口无效,重绘窗口

}

/*设备关闭*/
void dlg_main::OnClose()
{
	int cmd = CMD_DEVICE_CLOSE;
	::SendMessage(notify_wnd, MSG_DEVICE_CTRL, (WPARAM)NULL, (LPARAM)&cmd);
	__super::OnClose();
}

/*刷新设备状态*/
void dlg_main::OnBnClickedBtnRefresh()
{
	int cmd = CMD_DEVICE_REFRESH;
	// sendmessage 会将 消息 MSG_DEVICE_CTRL 以及 W PARAM 参数和 L PARAM 参数 发送到 notify_wnd窗口
	// 这里的 MSG_DEVICE_CTRL 是在spk_def.h里定义的, 相当于是一个标号
	// 这个标号在 scan.cpp 里被关联到 scan 的函数上
	::SendMessage(notify_wnd, MSG_DEVICE_CTRL, (WPARAM)NULL, (LPARAM)&cmd);
}


/*重启设备*/
void dlg_main::OnBnClickedBtnReboot()
{
	int cmd = CMD_DEVICE_REBOOT;
	::SendMessage(notify_wnd, MSG_DEVICE_CTRL, (WPARAM)NULL, (LPARAM)&cmd);
}
